package Year22_Month04.day23;

public class 移除链表元素_无虚拟节点版 {
    public static ListNode removeElements(ListNode head, int val) {
        if(head != null && head.val == val){
            head = head.next;
        }
        if(head != null){
            ListNode tmp = head;
            while(tmp.next != null){
                if(tmp.next.val == val){
                    tmp.next = tmp.next.next;
                }else{
                    tmp = tmp.next;
                }
            }
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(7);
        ListNode node2 = new ListNode(7);
        ListNode node3 = new ListNode(7);
        ListNode node4 = new ListNode(7);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        System.out.println(removeElements(node1,7));
    }
}
